from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton, QVBoxLayout, QWidget, QMenu, QAction, QMessageBox
from PyQt5.QtCore import Qt, QTimer, QPoint, QTime
from PyQt5.QtGui import QPainter, QColor, QPen
import pyttsx3

# 创建主窗口
class CountdownWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("倒计时器示例")
        self.setGeometry(100, 100, 100, 70)
        self.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.FramelessWindowHint)  # 修改: 添加 Qt.FramelessWindowHint 以实现无边框窗口
        self.setWindowOpacity(0.5)  # 设置窗口透明度（0.0 完全透明，1.0 完全不透明）

        # 倒计时变量
        self.countdown_time = None

        # 创建一个标签来显示倒计时
        self.label = QLabel("选择倒计时时长", self)
        self.label.setAlignment(Qt.AlignCenter)
        self.label.setStyleSheet("font-family: 'SimHei'; font-size: 30px; font-weight: bold; background-color: white;")

        # 创建按钮
        self.button_3_minutes = QPushButton("倒计时3分钟", self)
        self.button_3_minutes.clicked.connect(self.start_3_minutes_countdown)
        self.button_3_minutes.setVisible(False)  # 修改: 隐藏按钮

        self.button_5_minutes = QPushButton("倒计时5分钟", self)
        self.button_5_minutes.clicked.connect(self.start_5_minutes_countdown)
        self.button_5_minutes.setVisible(False)  # 修改: 隐藏按钮

        # 创建布局
        layout = QVBoxLayout()
        layout.addWidget(self.label)
        layout.addWidget(self.button_3_minutes)
        layout.addWidget(self.button_5_minutes)

        container = QWidget()
        container.setLayout(layout)
        self.setCentralWidget(container)

        # 创建定时器
        self.timer = QTimer()
        self.timer.timeout.connect(self.update_countdown)

        # 添加鼠标事件处理
        self.oldPos = None

        # 添加右键菜单
        self.setContextMenuPolicy(Qt.CustomContextMenu)
        self.customContextMenuRequested.connect(self.show_context_menu)

        # 添加背景动画定时器
        self.animation_timer = QTimer(self)
        self.animation_timer.timeout.connect(self.update)
        self.animation_timer.start(50)  # 每50毫秒更新一次

        # 初始化背景动画参数
        self.animation_offset = 0

    # 更新倒计时标签的函数
    def update_countdown(self):
        if self.countdown_time <= 0:
            self.label.setText("时间到！")
            self.reset_countdown()  # 倒计时结束后重置
            # 添加弹出框显示“时间到！”
            QMessageBox.information(self, "时间到", "时间到！")
        else:
            minutes, seconds = divmod(self.countdown_time, 60)
            self.label.setText(f"{minutes:02d}:{seconds:02d}")
            
            # 当倒计时达到1分钟时，更改标签背景颜色为橙色并播放语音
            if self.countdown_time == 60:
                self.label.setStyleSheet("font-family: 'SimHei'; font-size: 30px; font-weight: bold; background-color: orange;")
                self.speak_text("您还有1分钟")  # 播放语音
            
            self.countdown_time -= 1

    # 重置倒计时的函数
    def reset_countdown(self):
        if self.countdown_time is not None:
            self.timer.stop()
            self.countdown_time = None  # 重置变量
        self.label.setText("选择倒计时时长")  # 重置标签文本
        # 修改字体大小和样式
        self.label.setStyleSheet("font-family: 'SimHei'; font-size: 30px; font-weight: bold; background-color: white;")  # 重置标签背景颜色为白色

    # 开始3分钟倒计时的函数
    def start_3_minutes_countdown(self):
        self.reset_countdown()  # 重置倒计时
        self.label.setStyleSheet("font-family: 'SimHei'; font-size: 30px; font-weight: bold; background-color: white;")  # 修改: 确保字体大小和样式保持一致，并重置背景颜色
        self.countdown_time = 63  # 3分钟 = 180秒
        self.timer.start(1000)

    # 开始5分钟倒计时的函数
    def start_5_minutes_countdown(self):
        self.reset_countdown()  # 重置倒计时
        self.label.setStyleSheet("font-family: 'SimHei'; font-size: 30px; font-weight: bold; background-color: white;")  # 修改: 确保字体大小和样式保持一致，并重置背景颜色
        self.countdown_time = 5 * 60  # 5分钟 = 300秒
        self.timer.start(1000)

    # 语音播报函数
    def speak_text(self, text):
        engine = pyttsx3.init()  # 初始化pyttsx3引擎
        engine.setProperty('rate', 150)  # 设置语速
        engine.setProperty('volume', 0.8)  # 设置音量
        engine.say(text)  # 将文本添加到语音队列中
        engine.runAndWait()  # 等待语音队列中的所有语音播放完毕

    # 添加鼠标按下事件
    def mousePressEvent(self, event):
        self.oldPos = event.globalPos()

    # 添加鼠标移动事件
    def mouseMoveEvent(self, event):
        if self.oldPos:
            delta = QPoint(event.globalPos() - self.oldPos)
            self.move(self.x() + delta.x(), self.y() + delta.y())
            self.oldPos = event.globalPos()

    # 添加鼠标释放事件
    def mouseReleaseEvent(self, event):
        self.oldPos = None

    # 添加右键菜单显示按钮的选项
    def show_context_menu(self, position):
        context_menu = QMenu(self)

        # 添加新的右键菜单选项
        start_8_minutes_action = QAction("8分钟每日一学", self)
        start_8_minutes_action.triggered.connect(lambda: self.start_custom_countdown(8 * 60))
        context_menu.addAction(start_8_minutes_action)

        start_12_minutes_action = QAction("12分钟狱情分析", self)
        start_12_minutes_action.triggered.connect(lambda: self.start_custom_countdown(12 * 60))
        context_menu.addAction(start_12_minutes_action)

        start_3_minutes_action = QAction("3分钟科室研判", self)
        start_3_minutes_action.triggered.connect(lambda: self.start_custom_countdown(3 * 60))
        context_menu.addAction(start_3_minutes_action)

        start_1_min_3_sec_action = QAction("1分3秒测试", self)
        start_1_min_3_sec_action.triggered.connect(lambda: self.start_custom_countdown(1 * 60 + 3))
        context_menu.addAction(start_1_min_3_sec_action)

        close_action = QAction("关闭", self)
        close_action.triggered.connect(self.close)
        context_menu.addAction(close_action)

        context_menu.exec_(self.mapToGlobal(position))

    # 显示按钮的函数
    def show_buttons(self):
        self.button_3_minutes.setVisible(True)
        self.button_5_minutes.setVisible(True)

    # 添加鼠标进入事件处理
    def enterEvent(self, event):
        self.setWindowOpacity(1.0)  # 鼠标进入时设置窗口透明度为1.0

    # 添加鼠标离开事件处理
    def leaveEvent(self, event):
        self.setWindowOpacity(0.5)  # 鼠标离开时设置窗口透明度为0.5

    # 自定义绘制背景
    def paintEvent(self, event):
        painter = QPainter(self)
        painter.setPen(QPen(QColor(0, 255, 0), 2))  # 设置画笔颜色为绿色
        current_time = QTime.currentTime().msecsSinceStartOfDay()
        for i in range(0, self.width(), 10):
            y = (i + self.animation_offset) % self.height()
            painter.drawLine(i, y, i, y + 5)  # 绘制垂直线
        self.animation_offset += 1

    # 添加新的倒计时启动函数
    def start_custom_countdown(self, seconds):
        self.reset_countdown()  # 重置倒计时
        self.label.setStyleSheet("font-family: 'SimHei'; font-size: 30px; font-weight: bold; background-color: white;")  # 修改: 确保字体大小和样式保持一致，并重置背景颜色
        self.countdown_time = seconds
        self.timer.start(1000)

# 运行主循环
if __name__ == "__main__":
    app = QApplication([])
    window = CountdownWindow()
    window.show()
    app.exec_()